package com.cib.xagcjs.biz;

import com.alibaba.fastjson.JSONObject;
import com.cib.xagcjs.dal.EcnyLockMapper;
import com.cib.xagcjs.dal.MessageLogMapper;
import com.cib.xagcjs.dto.EcnyLockDTO;
import com.cib.xagcjs.dto.EcnyLockDetailDTO;
import com.cib.xagcjs.utils.CryptoUtils;
import com.cib.xagcjs.utils.ResponseUtil;
import com.cib.xagcjs.utils.TimeUtil;
import com.cib.xagcjs.vo.EcnyLockListVO;
import com.cib.xagcjs.vo.EcnyLockVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description: 数币冻结通知实现层
 * Author: jingcaiqi1225@gmail.com
 * Date: 2024/2/22 14:20
 */
@Service
@Slf4j
public class EcnyLockService {

    @Autowired
    private ResponseUtil responseUtil;

    @Autowired
    private CryptoUtils cryptoUtils;

    @Autowired
    private EcnyLockMapper ecnyLockMapper;

    @Autowired
    private MessageLogMapper messageLogMapper;


    /**
     * 区块链发起 数币冻结通知
     * @param req 请求
     * @return 执行结果
     */
    public String encyLock(String req) {
        String s = cryptoUtils.decryptAndVerify(req);
        EcnyLockDTO data = JSONObject.parseObject(s, EcnyLockDTO.class);

        // 此处写推送后台业务
        // 接收的通知数据发给后台处理 (此处为模拟)

        // 获取当前通知时间
        //yyyy-MM-dd HH:mm:ss
//        data.setNotifyTime(TimeUtil.getNowTime());
        log.info("推送报文日志打印：" + s);
        log.info("data数据日志打印：" + data);

        // 接收的消息入库通知主表
        if (messageLogMapper.insertMessageLog(4,"数币冻结指令通知",TimeUtil.getNowTime(),JSONObject.toJSONString(data)) > 0) {
            log.info("数币冻结指令通知入库成功!");
            if (ecnyLockMapper.insertLock(data) > 0) {
                log.info("数币冻结记录主表入库成功!");
                List<EcnyLockDetailDTO> assetLockList = data.getAssetLockList();
                for (EcnyLockDetailDTO ecnyLockDetailDTO : assetLockList) {
                    if (ecnyLockMapper.insertLockDetail(ecnyLockDetailDTO, data.getTransId()) > 0) {
                        log.info("{}数币冻结记录细表入库成功!", ecnyLockDetailDTO);
                    } else {
                        log.info("{}数币冻结记录细表入库失败!", ecnyLockDetailDTO);
                    }
                }
            } else {
                log.info("数币冻结记录主表入库失败!");
            }
        } else {
            log.info("数币冻结指令通知入库失败!");
        }


        try {
            return cryptoUtils.encryptAndSign(responseUtil.getResponse(data.getTransId(), "1", ""));
        } catch (Exception e) {
            log.error("加密报文失败", e);
            throw new RuntimeException(e);
        }
    }

    public String lockNotify(String req) {
        return null;
    }

    /**
     * 获取冻结主表冻结状态已经更改为 2 已冻结 列表
     * @param status 状态 已冻结
     * @return 列表
     */
    public List<EcnyLockVO> selectLock(int status) {
        return ecnyLockMapper.selectLock(status);
    }

    /**
     * 根据这个批次的transId 获取下面所有 状态 1-成功 的列表
     * @param transId 唯一交易ID
     * @return 列表
     */
    public List<EcnyLockListVO> selectLockList(String transId, int status) {
        return ecnyLockMapper.selectLockList(transId, status);
    }

    /**
     * 修改冻结通知表状态为 3 已反馈
     * @param transId 唯一交易ID
     * @param status 状态
     */
    public void updateLockStatus(String transId, int status) {
        ecnyLockMapper.updateLockStatus(transId, status);
    }
}
